package com.itheima.tlias84.controller;

import com.alibaba.druid.sql.visitor.functions.Now;
import com.itheima.tlias84.dto.SelectClazzDTO;
import com.itheima.tlias84.dto.SelectClazzDetailDTO;
import com.itheima.tlias84.dto.SelectClazzIDDTO;
import com.itheima.tlias84.dto.SelectEmpDTO;
import com.itheima.tlias84.mapper.ClazzMapper;
import com.itheima.tlias84.po.InsertClazzParam;
import com.itheima.tlias84.po.SelectClazzPageParam;
import com.itheima.tlias84.po.UpdateClazzParam;
import com.itheima.tlias84.servrce.ClazzService;
import com.itheima.tlias84.servrce.EmpService;
import com.itheima.tlias84.utils.ResultUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("clazzs")
public class ClazzController {
    @Resource
    ClazzService clazzService;
    @Resource
    EmpService empService;


    @PostMapping
    public ResultUtil insertClazz(@RequestBody InsertClazzParam in){
        int i = clazzService.insertClazz(in);
        return i==0?ResultUtil.fail("增加失败"):ResultUtil.success("增加成功");
    }
    //分页
    @GetMapping
     public ResultUtil selectClazzDetailDTO( SelectClazzPageParam param){
        if(Objects.isNull(param.getPage())||Objects.isNull(param.getPageSize())){
            return ResultUtil.fail("分页参数不存在");
        }
        ResultUtil resultUtil = clazzService.selectClazzDetailDTO(param);
        return resultUtil;
    }

    @DeleteMapping("{id}")
    public ResultUtil deleteById(@PathVariable("id") Integer id){
        int i = clazzService.deleteClazzById(id);
        return i==0?ResultUtil.fail("删除失败"):ResultUtil.success("删除成功");
    }

    //更新员工
    @PutMapping
    public ResultUtil updateClazzById(@RequestBody UpdateClazzParam updateClazzParam) {
        updateClazzParam.setUpdateTime(LocalDateTime.now());
        int i = clazzService.updateClazzById(updateClazzParam);
        return i==0?ResultUtil.fail("修改失败"):ResultUtil.success("修改成功");
    }

    //通过id查询
    @GetMapping("{id}")
    public ResultUtil selectClazzIDDTO(@PathVariable("id") Integer id){
        SelectClazzIDDTO selectClazzIDDTO = clazzService.selectClazzIDDTO(id);
        return ResultUtil.success(selectClazzIDDTO);
    }

    //查询所有班级
    @GetMapping("list")
    public ResultUtil selectClazzList(){
        List<SelectClazzDTO> selectClazzDTOS = clazzService.selectClazzList();
        return ResultUtil.success(selectClazzDTOS);
    }

}
